package com.qingge.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.common.Result;
import com.qingge.springboot.entity.Message;
import com.qingge.springboot.service.IMessageService;
import com.qingge.springboot.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author web_work
 * @since 2023-02-18
 */
@RestController
@RequestMapping("/message")
        public class MessageController {
    
    @Resource
    private IMessageService messageService;

    @Autowired
    private RedisService redisService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Message message) {
    messageService.saveOrUpdate(message);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
    messageService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
    messageService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(messageService.list());
        }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        Message message= messageService.getById(id);
        int value= redisService.getLikedMessageValueById(id);
        message.setLikeCount(message.getLikeCount()+value);
        return Result.success(message);
        }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(messageService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }



}

